package com.julyup.core.util;

import com.julyup.core.entity.Company;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author: zhenzhong.wang@honeywell.com
 * @Date: 2019/4/19/019 下午 10:38
 */
public class ExportUtils {

  private static String title[] = {
      "公司名称",
      "行业",
      "成立时间",
      "上线时长",
      "联系人",
      "手机",
      "固定电话",
      "其他电话",
      "经营模式",
      "公司官网",
      "公司地址",
      "数据来源"
  };

  public static Workbook create(List<Company> companys) {
    // 通过流创建XSSF WorkBook
    XSSFWorkbook wb = new XSSFWorkbook();

    // 目标sheet初始化
    Sheet sheet = wb.createSheet("上海");

    Row headerRow = sheet.createRow(0);
    for (int i = 0; i < title.length; i++) {
      Cell cell = headerRow.createCell(i);
      cell.setCellValue(title[i]);
    }

    for (int i = 0; i < companys.size(); i++) {
      Row dataRow = sheet.createRow(i + 1);
      dataRow.createCell(0).setCellValue(companys.get(i).getCompanyName());
      dataRow.createCell(1).setCellValue(companys.get(i).getIndustry());
      dataRow.createCell(2).setCellValue(companys.get(i).getRegTime());
      dataRow.createCell(3).setCellValue(companys.get(i).getOnlineTime());
      dataRow.createCell(4).setCellValue(companys.get(i).getContactPerson());
      dataRow.createCell(5).setCellValue(companys.get(i).getCellphone());
      dataRow.createCell(6).setCellValue(companys.get(i).getTelephone());
      dataRow.createCell(7).setCellValue(companys.get(i).getTelephone());
      dataRow.createCell(8).setCellValue(companys.get(i).getMode());
      dataRow.createCell(9).setCellValue(companys.get(i).getSite());
      dataRow.createCell(10).setCellValue(companys.get(i).getLocation());
      dataRow.createCell(11).setCellValue(companys.get(i).getOrigin());
    }

    return wb;
  }

  /**
   * 导出excel到文件系统
   *
   * @param companies
   * @param filePath
   * @throws IOException
   * @throws InvalidFormatException
   */
  public static void exportExcelToFile(List<Company> companies, String filePath) throws IOException{

    Workbook wb = create(companies);
    OutputStream out = new FileOutputStream(new File(filePath));
    wb.write(out);

    out.flush();
    out.close();
    wb.close();
  }

  /**
   * 导出excel到web
   *
   * @param companies
   * @param fileName
   * @param response
   * @throws IOException
   * @throws InvalidFormatException
   */
  public static void exportExcelToResponse(List<Company> companies, String fileName, HttpServletResponse response) throws IOException {
    Workbook wb = create(companies);
    String codedFileName;
    OutputStream resOut = null;
    try {
      // 如果文件名有中文，必须URL编码
      codedFileName = URLEncoder.encode(fileName, "UTF-8");
      response.reset();
      // ContentType 可以不设置
      response.setContentType("application/vnd.ms-excel;charset=UTF-8");
      response.setHeader("Content-Disposition", "attachment;filename=" + codedFileName);
      wb.write(response.getOutputStream());
      resOut = response.getOutputStream();
    } catch (Exception e) {

    } finally {
      resOut.flush();
      resOut.close();
    }
  }
}
